home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2050 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  131 lines

  1. Path: news.pi.net!Asd18.pi.net
  2. From: mv@pi.net
  3. Newsgroups: comp.lang.c
  4. Subject: Please help ?!
  5. Date: Thu, 18 Jan 96 20:46:55
  6. Organization: Planet Internet
  7. Message-ID: <4dm889$3hs@neptunus.pi.net>
  8. NNTP-Posting-Host: asd18.pi.net
  9. X-Newsreader: IBM WebExplorer DLL 
  10.  
  11. Hello everybody,
  12.  
  13.  
  14. Please take a look at this:
  15.  
  16. ----------------------------
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <conio.h>
  20. #include <string.h>
  21. #include <alloc.h>
  22.  
  23.  
  24. #define MAXLEN    20
  25. #define MAXSWITCH 20
  26.  
  27.  
  28. char *s="dir /w:test/1/2/3/4";
  29. char *s2, *s3;
  30. char myswitches[MAXSWITCH][MAXLEN];
  31.  
  32.  
  33. int get_switches(char *cmd, char *switches[][MAXLEN])
  34.  
  35.  
  36. {
  37.  int i=0;
  38.  char *cpy;
  39.  char *saved;
  40.  
  41.  if (strlen(cmd)==0)
  42.   {
  43.    return 0;
  44.   }
  45.   cpy=(char *)malloc(MAXLEN);
  46.    if (cpy==NULL)
  47.     {
  48.      printf("Out of memory in %s on line: %d\n",__FILE__,__LINE__);
  49.      exit(EXIT_FAILURE);
  50.     }
  51.    strcpy(cpy,'\0');
  52.     saved=cpy;
  53.   while (*cmd!='\0')
  54.    {
  55.     if (*cmd=='/')
  56.      {
  57.       if (i==MAXSWITCH)
  58.        {
  59.         printf("INT: Too many switches ...\n");
  60.         return 9999;
  61.        }
  62.       *cpy=*cmd;
  63.       do { *cpy++=*cmd++; } while (!strchr(" /-\0",*cmd));
  64.         *cpy='\0';
  65.          cpy=saved;
  66.           strcpy(switches[i],cpy);
  67.         strcpy(cpy,'\0');
  68.        i++;
  69.      }
  70.     if (*cmd=='-')
  71.     {
  72.      if (i==MAXSWITCH)
  73.        {
  74.         printf("INT: Too many switches ...\n");
  75.         return 9999;
  76.        }
  77.      *cpy=*cmd;
  78.       do { *cpy++=*cmd++; } while (!strchr(" /-\0",*cmd));
  79.        *cpy='\0';
  80.         cpy=saved;
  81.          strcpy(switches[i],cpy);
  82.        strcpy(cpy,'\0');
  83.        i++;
  84.      }
  85.  if (!strchr("/-\0",*cmd)) {*cmd++;}
  86.    }
  87.   free(cpy);
  88.  return i;
  89. }
  90.  
  91.  
  92. int main (void)
  93.  
  94.  
  95. {
  96.  int i;
  97.  clrscr();
  98.   i=get_switches(s,myswitches);
  99.    printf("Switches found: %i\n",i);
  100.    for (i=0; i<MAXSWITCH; i++)
  101.     {
  102.  
  103.  
  104. /*
  105.  
  106.  Here it should print:
  107.  
  108. Switch: /w:test
  109. Switch: /1
  110. etc..
  111.  
  112. but it prints:
  113.  
  114. Switch: /w:test
  115. Switch: 
  116. Switch: /1
  117. and so on....
  118. It jjust skips one array index everytime... It seems to go wrong in get_switches()...
  119. */
  120.      printf("Switch: %s\n",myswitches[i]);
  121.     }
  122.  return 0;
  123. }
  124.  
  125.  
  126.  
  127. TTYL,
  128.  
  129. Martijn....
  130.  
  131.